#include "viewlock.h"
#include "ui_viewlock.h"
#include "AES.h"
#include "mainwindow.h"
#include "editlock.h"

//每16个字符解密一次
QString my_decrypt(AESModeOfOperation *moo, unsigned char *input, int n)
{
    int round = n%16>0 ? n/16+1 : n/16;
    int len = 0;
    unsigned char *output = (unsigned char *)malloc(n);
    unsigned char *tmp = output;
    int tmp_n = n;
    memset(output, 0, n);
    do {
        len = n>16 ? moo->Decrypt(input, 16, output) : moo->Decrypt(input, n, output);
        output = output+len;
        input = input+len;
        round--;
        n = n-len;
    }while(round > 0);
    QString result = UnsignedChar2QString(tmp, tmp_n);
    free(tmp);
    return result;
}

viewlock::viewlock(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::viewlock)
{
    ui->setupUi(this);
}

viewlock::viewlock(QString password, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::viewlock)
{
    ui->setupUi(this);
    this->password = password;
    //AES解密
    //0. 读取路径
    QFile file("path.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
    QString path;
    QTextStream in(&file);
    while (!in.atEnd())
    {
        QString line = in.readLine();
        path += line;
    }
    this->path = path;
    file.close();
    //1. 解密
    QFile password_file(path+"password.txt");
    if (!password_file.open(QIODevice::ReadOnly)) return;
    QDataStream in1(&password_file);
    unsigned char tmp;
    int n = 0;
    while (!in1.atEnd())
    {
        in1 >> tmp;
        n++;
    }
    password_file.close();
    unsigned char *all = (unsigned char *)malloc(n);
    QFile password_file1(path+"password.txt");
    if (!password_file1.open(QIODevice::ReadOnly)) return;
    QDataStream in2(&password_file1);
    int cnt = 0;
    while (!in2.atEnd())
    {
        in2 >> all[cnt];
        cnt++;
    }
    unsigned char iv[] = {103,35,148,239,76,213,47,118,255,222,123,176,106,134,98,92};
    unsigned char *key = QString2Key(password);

    AESModeOfOperation moo;
    moo.set_key(key);
    moo.set_mode(MODE_OFB);
    moo.set_iv(iv);
    QString str_output = my_decrypt(&moo, all, n);
    ui->viewlockEdit->setText(str_output);
    free(key);
    free(all);
}

viewlock::~viewlock()
{
    delete ui;
}


void viewlock::on_backButton_clicked()
{
    this->close();
    emit back_password_signal();
}

void viewlock::on_resaveButton_clicked()
{
    QString str = ui->viewlockEdit->toPlainText();
    unsigned char iv[] = {103,35,148,239,76,213,47,118,255,222,123,176,106,134,98,92};
    unsigned char *input = QString2Input(str);
    unsigned char *key = QString2Key(password);

    AESModeOfOperation moo;
    moo.set_key(key);
    moo.set_mode(MODE_OFB);
    moo.set_iv(iv);

    int length = 0;
    QByteArray ba = str.toLocal8Bit();
    char *ptr = ba.data();
    while (*ptr != 0)
    {
        ptr++;
        length++;
    }
    unsigned char *output = (unsigned char *)malloc(length);
    my_encrypt(&moo, input, length, path);
    free(output);
}
